package cn.portx.view;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import com.bstek.dorado.annotation.DataProvider;

@Component
public class VesselDynamicPR {

	@Autowired
	private RestTemplate restTemplate;

	@DataProvider
	@Transactional
	public List<Map<String, Object>> loadShipDynamic(Map<String, Object> params) {
		String voyage = (String) params.get("voyage");
		String shipName = (String) params.get("shipName");

		String url = "http://api.portx.cn/commonapi/ship/dynamic/?/?";
		url = StringUtils.replaceFirst(url, "\\?", shipName);
		url = StringUtils.replaceFirst(url, "\\?", voyage);

		Map<String, Object> resultMap = this.restTemplate.getForObject(url, Map.class);
		int code = Integer.parseInt(resultMap.get("code").toString());
		if (code == 0) {
			//成功
			List<Map<String, Object>> resultList = (List<Map<String, Object>>) resultMap.get("result");
			return resultList;
		} else {
			String errorMsg = (String) resultMap.get("msg");
			throw new RuntimeException(errorMsg);
		}
	}
}
